<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel数据导入测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            color: #333;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 900px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
        }
        .tabs {
            display: flex;
            margin-bottom: 20px;
            border-bottom: 1px solid #ddd;
        }
        .tab {
            padding: 10px 20px;
            cursor: pointer;
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-bottom: none;
            border-radius: 5px 5px 0 0;
            margin-right: 5px;
        }
        .tab.active {
            background-color: #fff;
            border-bottom: 1px solid #fff;
            margin-bottom: -1px;
            font-weight: bold;
        }
        .tab-content {
            display: none;
        }
        .tab-content.active {
            display: block;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        select, input[type="file"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #2980b9;
        }
        .btn-secondary {
            background-color: #95a5a6;
        }
        .btn-secondary:hover {
            background-color: #7f8c8d;
        }
        .actions {
            margin-top: 30px;
            text-align: center;
        }
        .loading {
            display: none;
            text-align: center;
            margin-top: 20px;
        }
        .loading-spinner {
            display: inline-block;
            width: 30px;
            height: 30px;
            border: 3px solid rgba(0, 0, 0, 0.1);
            border-radius: 50%;
            border-top-color: #3498db;
            animation: spin 1s ease-in-out infinite;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        .result {
            display: none;
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .file-upload {
            position: relative;
            overflow: hidden;
            margin-top: 10px;
        }
        .file-upload input[type="file"] {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
            cursor: pointer;
        }
        .file-upload-label {
            display: inline-block;
            padding: 8px 20px;
            background-color: #e9ecef;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-weight: normal;
            cursor: pointer;
        }
        .file-name {
            margin-left: 10px;
        }
        .back-link {
            display: inline-block;
            margin-top: 30px;
            color: #3498db;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Excel 数据导入测试</h1>
        
        <div class="tabs">
            <div class="tab active" data-tab="single">单Sheet导入</div>
            <div class="tab" data-tab="multi">多Sheet导入</div>
        </div>
        
        <div class="tab-content active" id="single-tab">
            <h2>单Sheet Excel导入</h2>
            <p>选择业务类型并上传单Sheet的Excel文件进行导入测试。</p>
            
            <div class="form-group">
                <label for="single-business-type">业务类型</label>
                <select id="single-business-type">
                    <option value="INVOICE">发票数据</option>
                    <option value="CUST_CREDIT">客户授信</option>
                    <option value="CUST_PAYMENT_PLAN">客户付款计划</option>
                    <option value="CUST_PAYMENT_RESULT">客户付款结果</option>
                </select>
            </div>
            
            <div class="form-group">
                <label>Excel文件</label>
                <div class="file-upload">
                    <label class="file-upload-label">选择文件</label>
                    <span class="file-name" id="single-file-name">未选择文件</span>
                    <input type="file" id="single-file" accept=".xlsx,.xls">
                </div>
            </div>
            
            <div class="actions">
                <button id="single-import-btn" class="btn">开始导入</button>
            </div>
        </div>
        
        <div class="tab-content" id="multi-tab">
            <h2>多Sheet Excel导入</h2>
            <p>上传包含多个Sheet的Excel文件，并指定每个Sheet对应的业务类型。</p>
            
            <div class="form-group">
                <label for="multi-business-types">业务类型列表</label>
                <select id="multi-business-types" multiple size="4">
                    <option value="CUST_CREDIT">客户授信</option>
                    <option value="INVOICE">发票数据</option>
                    <option value="CUST_PAYMENT_PLAN">客户付款计划</option>
                    <option value="CUST_PAYMENT_RESULT">客户付款结果</option>
                </select>
                <p class="help-text">按住Ctrl键可多选，每个选项对应Excel中的一个Sheet，顺序必须一致</p>
            </div>
            
            <div class="form-group">
                <label>Excel文件</label>
                <div class="file-upload">
                    <label class="file-upload-label">选择文件</label>
                    <span class="file-name" id="multi-file-name">未选择文件</span>
                    <input type="file" id="multi-file" accept=".xlsx,.xls">
                </div>
            </div>
            
            <div class="form-group">
                <div style="margin-top: 8px;">
                    <input type="checkbox" id="enableTransaction">
                    <label for="enableTransaction" style="display: inline;">启用事务管理（所有sheet在同一事务中处理，有错误时回滚所有操作）</label>
                </div>
            </div>
            
            <div class="actions">
                <button id="multi-import-btn" class="btn">开始导入</button>
            </div>
        </div>
        
        <div class="loading">
            <div class="loading-spinner"></div>
            <p>正在处理，请稍候...</p>
        </div>
        
        <div class="result" id="result-box"></div>
        
        <div style="text-align: center;">
            <a href="excel-import-guide.html" class="btn btn-secondary">查看使用指南</a>
            <a href="index.html" class="back-link">返回主页</a>
        </div>
    </div>
    
    <script>
        // 切换标签页
        document.querySelectorAll('.tab').forEach(tab => {
            tab.addEventListener('click', () => {
                // 移除所有标签页和内容的active类
                document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
                document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
                
                // 添加active类到当前标签页和对应内容
                tab.classList.add('active');
                document.getElementById(tab.getAttribute('data-tab') + '-tab').classList.add('active');
            });
        });
        
        // 文件选择显示
        document.getElementById('single-file').addEventListener('change', function() {
            document.getElementById('single-file-name').textContent = this.files[0] ? this.files[0].name : '未选择文件';
        });
        
        document.getElementById('multi-file').addEventListener('change', function() {
            document.getElementById('multi-file-name').textContent = this.files[0] ? this.files[0].name : '未选择文件';
        });
        
        // 单Sheet导入
        document.getElementById('single-import-btn').addEventListener('click', function() {
            const businessType = document.getElementById('single-business-type').value;
            const fileInput = document.getElementById('single-file');
            
            if (!fileInput.files || fileInput.files.length === 0) {
                showResult('请选择Excel文件', false);
                return;
            }
            
            const formData = new FormData();
            formData.append('businessType', businessType);
            formData.append('file', fileInput.files[0]);
            
            showLoading(true);
            
            fetch('/common/importExcel', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showLoading(false);
                if (data.success) {
                    showResult('导入成功！', true);
                } else {
                    showResult('导入失败: ' + data.msg, false);
                }
            })
            .catch(error => {
                showLoading(false);
                showResult('导入失败: ' + (error.message || '未知错误'), false);
            });
        });
        
        // 多Sheet导入
        document.getElementById('multi-import-btn').addEventListener('click', function() {
            const businessTypesSelect = document.getElementById('multi-business-types');
            const fileInput = document.getElementById('multi-file');
            const enableTransaction = document.getElementById('enableTransaction').checked;
            
            // 获取选中的业务类型
            const selectedTypes = Array.from(businessTypesSelect.selectedOptions).map(option => option.value);
            
            if (selectedTypes.length === 0) {
                showResult('请选择至少一个业务类型', false);
                return;
            }
            
            if (!fileInput.files || fileInput.files.length === 0) {
                showResult('请选择Excel文件', false);
                return;
            }
            
            // 将业务类型数组转为逗号分隔的字符串
            const businessTypes = selectedTypes.join(',');
            
            const formData = new FormData();
            formData.append('businessTypes', businessTypes);
            formData.append('file', fileInput.files[0]);
            formData.append('enableTransaction', enableTransaction);
            
            showLoading(true);
            
            fetch('/common/importMultiSheetExcel', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showLoading(false);
                if (data.success) {
                    showResult('多Sheet导入成功！', true);
                } else {
                    showResult('导入失败: ' + data.msg, false);
                }
            })
            .catch(error => {
                showLoading(false);
                showResult('导入失败: ' + (error.message || '未知错误'), false);
            });
        });
        
        // 辅助函数
        function showLoading(show) {
            document.querySelector('.loading').style.display = show ? 'block' : 'none';
        }
        
        function showResult(message, isSuccess) {
            const resultBox = document.getElementById('result-box');
            resultBox.textContent = message;
            resultBox.style.display = 'block';
            resultBox.className = 'result ' + (isSuccess ? 'success' : 'error');
            
            // 5秒后自动隐藏结果
            setTimeout(() => {
                resultBox.style.display = 'none';
            }, 5000);
        }
    </script>
</body>
</html> 